package Review;

public class Test20 {

	public static void main(String[] args) {
		int[][] matrix={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
		printMatrix(matrix);
	}
    public static void printMatrix(int[][] matrix){
    	int rows=matrix.length;
    	int columns=matrix[0].length;
    	if(matrix==null||rows<=0||columns<=0){
    		return;
    	}
    	int start=0;
    	while(rows>2*start&&columns>2*start){
    		printCircle(matrix,rows,columns,start);
    		start++;
    	}
    }
    public static void printCircle(int[][]matrix,int rows,int columns,int start){
    	int endX=rows-start-1;
    	int endY=columns-start-1;
    	for(int i =start;i<=endY;i++){
    		System.out.print(matrix[start][i]);
    		System.out.print(" ");
    	}
    	if(endX>start){
    		for(int i=start+1;i<=endX;i++){
    			System.out.print(matrix[i][endY]);
    			System.out.print(" ");
    		}
    	}
    	if(endX>start&&endY>start){
    		for(int i=endY-1;i>=start;i--){
    			System.out.print(matrix[endX][i]);
    			System.out.print(" ");
    		}
    	}
    	if(endX>start+1&&endY>start){
    		for(int i=endX-1;i>=start+1;i--){
    			System.out.print(matrix[i][start]);
    			System.out.print(" ");
    		}
    	}
    }
 
}
